package ph.com.gs3.formalistics.model.dao;

import ph.com.gs3.formalistics.model.tables.PendingActionsTable;
import ph.com.gs3.formalistics.model.valueobjects.application.PendingAction;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

public class PendingActionsDAO extends SQLiteDataAccessObject {

	private String[] columns;

	public PendingActionsDAO(Context context) {
		super(context);

		columns = new PendingActionsTable().getTableColumns();
	}

	public PendingAction savePendingAction(PendingAction action) {

		ContentValues cv = createCVFromPendingAction(action);

		long insertId = database.insert(PendingActionsTable.NAME, null, cv);
		if (insertId > 0) {
			return getPendingActionById(Long.toString(insertId));
		} else {
			throw new SQLException("Saving of pending action failed");
		}

	}

	public void removePendingAction(String id) {

		String whereClause = PendingActionsTable.COL_ID + "=?";
		String[] whereArgs = { id };

		long affectedRows = database.delete(PendingActionsTable.NAME, whereClause, whereArgs);
		if (affectedRows <= 0) {
			throw new SQLException("Failed to delete pending action: " + id);
		}

	}

	public PendingAction getPendingActionById(String dbId) {

		String whereClause = PendingActionsTable.COL_ID + "=?";
		String[] whereArgs = { dbId };
		String orderByClause = PendingActionsTable.COL_DATE_ISSUED + " DESC";

		Cursor cursor = database.query(PendingActionsTable.NAME, columns, whereClause, whereArgs,
		        null, null, orderByClause);

		PendingAction action = null;

		cursor.moveToFirst();
		if (!cursor.isAfterLast()) {
			action = cursorToPendingAction(cursor);
		}

		cursor.close();

		return action;

	}

	// ================================================================================================
	// {{ Parsers

	private PendingAction cursorToPendingAction(Cursor cursor) {

		// @formatter:off
		int idIndex 				= cursor.getColumnIndexOrThrow(PendingActionsTable.COL_ID);
		int actionNameIndex 		= cursor.getColumnIndexOrThrow(PendingActionsTable.COL_ACTION_NAME);
		int actionDataIndex 		= cursor.getColumnIndexOrThrow(PendingActionsTable.COL_ACTION_NAME);
		
		int destinationServerIndex 	= cursor.getColumnIndexOrThrow(PendingActionsTable.COL_DESTINATION_SERVER);
		int issuedByUserIdIndex 	= cursor.getColumnIndexOrThrow(PendingActionsTable.COL_ISSUED_BY_USER_ID);
		
		int relatedDataDbIdIndex 	= cursor.getColumnIndexOrThrow(PendingActionsTable.COL_RELATED_DATA_DB_ID);
		int relatedDataDescIndex 	= cursor.getColumnIndexOrThrow(PendingActionsTable.COL_RELATED_DATA_DESC);
		
		int dateIssuedIndex 		= cursor.getColumnIndexOrThrow(PendingActionsTable.COL_DATE_ISSUED);
		// @formatter:on

		// @formatter:off
		String id 					= cursor.getString(idIndex);
		String actionName			= cursor.getString(actionNameIndex);
		String actionData 			= cursor.getString(actionDataIndex);
		
		String destinationServer 	= cursor.getString(destinationServerIndex);
		String issuedByUserId 		= cursor.getString(issuedByUserIdIndex);
		
		String relatedDataDbId 		= cursor.getString(relatedDataDbIdIndex);
		String relatedDataDesc 		= cursor.getString(relatedDataDescIndex);
		
		String dateIssued 			= cursor.getString(dateIssuedIndex);
		// @formatter:on

		PendingAction action = new PendingAction();

		action.setId(id);
		action.setActionName(actionName);
		action.setActionData(actionData);

		action.setDestinationServer(destinationServer);
		action.setIssuedByUserId(issuedByUserId);

		action.setRelatedDataDbId(relatedDataDbId);
		action.setRelatedDataDesc(relatedDataDesc);

		action.setDateIssued(dateIssued);

		return action;

	}

	private ContentValues createCVFromPendingAction(PendingAction action) {

		ContentValues cv = new ContentValues();

		cv.put(PendingActionsTable.COL_ACTION_NAME, action.getActionName());
		cv.put(PendingActionsTable.COL_ACTION_DATA, action.getActionData());
		cv.put(PendingActionsTable.COL_DESTINATION_SERVER, action.getDestinationServer());
		cv.put(PendingActionsTable.COL_ISSUED_BY_USER_ID, action.getIssuedByUserId());
		cv.put(PendingActionsTable.COL_RELATED_DATA_DB_ID, action.getRelatedDataDbId());
		cv.put(PendingActionsTable.COL_RELATED_DATA_DESC, action.getRelatedDataDesc());
		cv.put(PendingActionsTable.COL_DATE_ISSUED, action.getDateIssued());

		return cv;

	}

	// }}

}
